package artid.com.arttogo.activity;
/**
 * 拍品搜索
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.AucOrgListAdapter;
import artid.com.arttogo.adapter.AuctionPastAuction_Adapter;
import artid.com.arttogo.adapter.LotListAdapter;
import artid.com.arttogo.adapter.LotSearchInputAdapter;
import artid.com.arttogo.adapter.SpecListAdapter;
import artid.com.arttogo.bean.AuctionAuctionListParser;
import artid.com.arttogo.bean.JigouList;
import artid.com.arttogo.bean.LotSearchDet;
import artid.com.arttogo.bean.SearchList;
import artid.com.arttogo.bean.Spinfo;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MySharedPreferences;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.MyListView;

public class LotSearchActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {
	public static final String LOTSRH_TAG = "lotsrhtag";
	/**
	 * 顶部搜索
	 */
	private ImageView back;
	ImageView mimgMirrorCenter, mimgMirrorLeft;
	EditText meditSearch;
	View viewSearch;
	TextView textSearchTag, textcancel;
	Boolean isSearching = false;
	LinearLayout linearSearch;
	private int anmiSpeed = 400;
	View titleSearch;
	String searchWord = "";

	String name = "";
	String id = "";
	LinearLayout mMainLinearLayout;
	String resultMain = "";
	private Button clearBtn;

	MyListView mListView;
	LotSearchInputAdapter mAdapter;
	MySharedPreferences mShare;
	String strListData;

	MyListView mListLot,mListSpec,mListAuc,mListOrg;
	LotListAdapter mAdapterLot;
	SpecListAdapter mAdapterSpec;
	AucOrgListAdapter mAdapterOrg;
	AuctionPastAuction_Adapter mAdapterAuc;
	View mViewBottom,mViewTop;
	View mViewLotMore,mViewAucMore,mViewSpecMore,mViewOrgMore;
	private LinearLayout noDataLL,noDataLL_include;
	private String imgwide;// 请求的图片宽度
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lot_search);
		mContext = LotSearchActivity.this;
		imgwide = (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 1) + "";
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		meditSearch = (EditText) findViewById(R.id.editText1);
		mShare = MySharedPreferences.getMySharedPreferences(mContext);
		noDataLL = (LinearLayout) findViewById(R.id.lot_search_noDataLL);
		noDataLL_include = (LinearLayout) findViewById(R.id.ll_hint);
		noDataLL_include.setVisibility(View.VISIBLE);
		mViewAucMore = (View)findViewById(R.id.clt_srh_auction_more);
		mViewAucMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,LotSearchDetActivity.class);
				intent.putExtra("type", "2");
				intent.putExtra("keyword", meditSearch.getText().toString());
				startActivity(intent);
			}
		});
		mViewLotMore = (View)findViewById(R.id.clt_srh_lot_more);
		mViewLotMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,LotSearchDetActivity.class);
				intent.putExtra("type", "4");
				intent.putExtra("keyword", meditSearch.getText().toString());
				startActivity(intent);
			}
		});
		mViewSpecMore = (View)findViewById(R.id.clt_srh_spec_more);
		mViewSpecMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,LotSearchDetActivity.class);
				intent.putExtra("type", "3");
				intent.putExtra("keyword", meditSearch.getText().toString());
				startActivity(intent);
			}
		});
		mViewOrgMore = (View)findViewById(R.id.clt_srh_organ_more);
		mViewOrgMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,LotSearchDetActivity.class);
				intent.putExtra("type", "1");
				intent.putExtra("keyword", meditSearch.getText().toString());
				startActivity(intent);
			}
		});
		mListLot = (MyListView)findViewById(R.id.clt_srh_lot_list);
		mListSpec = (MyListView)findViewById(R.id.clt_srh_spec_list);
		mListAuc = (MyListView)findViewById(R.id.clt_srh_auction_list);
		mListOrg = (MyListView)findViewById(R.id.clt_srh_organ_list);
		mListView = (MyListView)findViewById(R.id.clt_srh_list);
		mMainLinearLayout = (LinearLayout) findViewById(R.id.search_main);
		mViewBottom = (View)findViewById(R.id.clt_srh_bottom_det);
		mViewBottom.setVisibility(View.GONE);
		mViewTop = (View)findViewById(R.id.search_topview);
		meditSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		meditSearch
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					public boolean onEditorAction(TextView v, int actionId,
												  KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE
								|| (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
							if (!TextUtils.isEmpty(v.getText().toString())) {
								reqData(v.getText().toString());
								String strall = mShare.getValue(LOTSRH_TAG, "");
								if (strall.contains(v.getText().toString())) {
									return true;
								}
								strall += (v.getText().toString() + "#");
								mShare.setValue(LOTSRH_TAG, strall);
								initData();
							}else{
								ToastUtil.toast(mContext,"您还没有输入关键字呢!!!");
							}

						}
						return false;
					}
				});
		titleSearch = (View) findViewById(R.id.collecter_serach);
		textcancel = (TextView) findViewById(R.id.searchcancel);
		textcancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		// titleSearch.setOnClickListener(searchlistener());
		back = (ImageView) findViewById(R.id.title_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		clearBtn = (Button) findViewById(R.id.clt_srh_clear);
		clearBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 清空条件
				mShare.setValue(LOTSRH_TAG, "");
				initData();
			}
		});
		initData();
	}
	/**
	 *
	 * 搜索历史列表
	 *
	 */
	private void initData() {
		strListData = mShare.getValue(LOTSRH_TAG, "");
		ArrayList<String> listdata = new ArrayList<String>();
		ArrayList<String> listInput = new ArrayList<>();
		if(!StringUtils.isEmpty(strListData)) {
			String[] strs = strListData.split("#");
			if (strs.length < 1) {
				return;
			}

			for (String str:strs) {
				listdata.add(str);
			}
			listInput = listdata;
		}
		mAdapter = new LotSearchInputAdapter(mContext, listdata);
		mListView.setAdapter(mAdapter);
		final ArrayList<String> finalListInput = listInput;
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				meditSearch.setText(finalListInput.get(arg2));
				openInput();
				reqData(finalListInput.get(arg2));
			}
		});
	}

	/**
	 *
	 * 键盘弹出，回车点击搜索，请求
	 * @param keywork
	 */
	void reqData(String keywork){
		mViewBottom.setVisibility(View.VISIBLE);
		VolleyBasic.getInstance((Activity)mContext).post(Cache.searchList,
				VolleyMap.getInstance().searchList(keywork,"1",imgwide), SearchList.class,
				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						mViewTop.setVisibility(View.GONE);
						SearchList item = (SearchList)vd.getData();
						if (item.getListInfo().size()>3) {
							ArrayList<LotSearchDet> list = new ArrayList<LotSearchDet>();
							list.add(item.getListInfo().get(0));
							list.add(item.getListInfo().get(1));
							list.add(item.getListInfo().get(2));
							item.setListInfo((ArrayList<LotSearchDet>) item.getListInfo().subList(0, 3));
						}
						if (item.getListJginfo().size()>3) {
							ArrayList<JigouList> list = new ArrayList<JigouList>();
							list.add(item.getListJginfo().get(0));
							list.add(item.getListJginfo().get(1));
							list.add(item.getListJginfo().get(2));
							item.setListJginfo(list);
						}
						if (item.getListAuctioninfo().size()>3) {
							ArrayList<AuctionAuctionListParser> list = new ArrayList<AuctionAuctionListParser>();
							list.add(item.getListAuctioninfo().get(0));
							list.add(item.getListAuctioninfo().get(1));
							list.add(item.getListAuctioninfo().get(2));
							item.setListAuctioninfo(list);
						}
						if (item.getListSpinfo().size()>3) {
							ArrayList<Spinfo> list = new ArrayList<Spinfo>();
							list.add(item.getListSpinfo().get(0));
							list.add(item.getListSpinfo().get(1));
							list.add(item.getListSpinfo().get(2));
							item.setListSpinfo(list);
						}
						mAdapterAuc = new AuctionPastAuction_Adapter(mContext, item.getListAuctioninfo());
						mAdapterLot = new LotListAdapter(mContext);
						mAdapterLot.setData(item.getListInfo());
						mAdapterOrg = new AucOrgListAdapter(mContext);
						mAdapterOrg.setData(item.getListJginfo());
						mAdapterSpec = new SpecListAdapter(mContext);
						mAdapterSpec.setData(item.getListSpinfo());
						mListAuc.setAdapter(mAdapterAuc);
						mListLot.setAdapter(mAdapterLot);
						mListOrg.setAdapter(mAdapterOrg);
						mListSpec.setAdapter(mAdapterSpec);
						mListAuc.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
													View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								Intent intent =new Intent(mContext,AuctionInfo_Activity.class);
								intent.putExtra("auctionID", mAdapterAuc.list.get(arg2).getId());
								intent.putExtra("SpecialListData", 0);
								startActivity(intent);
							}
						});
						mListLot.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
													View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
                                //拍品详情
								Intent intent =new Intent(mContext,LotDetailPagerActivity.class);
								intent.putExtra("id", mAdapterLot.list.get(arg2).getId());
								startActivity(intent);
							}
						});
						mListOrg.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
													View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								Intent intent =new Intent(mContext,AucOrgDetActivity.class);
								intent.putExtra("id", mAdapterOrg.list.get(arg2).getId());
								startActivity(intent);
							}
						});
						mListSpec.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
													View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								Intent intent =new Intent(mContext,AuctionSpecialInfo_Activity.class);
								intent.putExtra("SpecialID", mAdapterSpec.list.get(arg2).getId());
								startActivity(intent);
							}
						});
						if (item.getListAuctioninfo().size()<1) {
							mViewAucMore.setVisibility(View.GONE);
						}else {
							mViewAucMore.setVisibility(View.VISIBLE);
						}

						if (item.getListInfo().size()<1) {
							mViewLotMore.setVisibility(View.GONE);
						}else {
							mViewLotMore.setVisibility(View.VISIBLE);
						}

						if (item.getListJginfo().size()<1) {
							mViewOrgMore.setVisibility(View.GONE);
						}else {
							mViewOrgMore.setVisibility(View.VISIBLE);
						}

						if (item.getListSpinfo().size()<1) {
							mViewSpecMore.setVisibility(View.GONE);
						}else {
							mViewSpecMore.setVisibility(View.VISIBLE);
						}


						if(mViewAucMore.getVisibility()==View.VISIBLE || mViewLotMore.getVisibility()==View.VISIBLE||
								mViewOrgMore.getVisibility()==View.VISIBLE || mViewOrgMore.getVisibility()==View.VISIBLE){
							noDataLL.setVisibility(View.GONE);
						}else{
							noDataLL.setVisibility(View.VISIBLE);
						}

					}
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public void onClick(View v) {

	}




	void openInput() {
		InputMethodManager m = (InputMethodManager) meditSearch.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
	}



	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}
}
